package libase.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author aspr0
 */
@Entity
@Table (name = "EXEMPLAR")
@XmlRootElement
@NamedQueries (
        {
    @NamedQuery (name = "Exemplar.findAll", query = "SELECT e FROM Exemplar e ORDER BY e.id"),
    @NamedQuery (name = "Exemplar.findById", query = "SELECT e FROM Exemplar e WHERE e.id = :id"),
    @NamedQuery (name = "Exemplar.findByBarcode", query = "SELECT e FROM Exemplar e WHERE e.barcode = :barcode"),
    @NamedQuery (name = "Exemplar.findByBook",
        query = "SELECT e FROM Exemplar e WHERE e.book = :book"),
    @NamedQuery (name = "Exemplar.findAvailableByBook",
        query =
        "SELECT e FROM Exemplar e "
        + "WHERE e.book = :book "
        + "and ( "
        + "select count(l) "
        + "from Lease l "
        + "where l.exemplar = e "
        + "and l.completed = 'N') = 0"),
    @NamedQuery (name = "Exemplar.findLeasedByBook",
        query =
        "SELECT e FROM Exemplar e "
        + "WHERE e.book = :book "
        + "and ( "
        + "select count(l) "
        + "from Lease l "
        + "where l.exemplar = e "
        + "and l.completed = 'Y') = 1"),
    @NamedQuery (name = "Exemplar.countAvailable",
        query =
        "SELECT COUNT(e) "
        + "FROM Exemplar e "
        + "WHERE "
        + "e.book = :book and "
        + "(select count(l) "
        + "from Lease l "
        + "where l.exemplar = e and "
        + "l.completed = 'N') = 0"),
    @NamedQuery (name = "Exemplar.countLeased",
        query =
        "SELECT COUNT(e) "
        + "FROM Exemplar e "
        + "WHERE "
        + "e.book = :book and "
        + "(select count(l) "
        + "from Lease l "
        + "where l.exemplar = e and "
        + "l.completed = 'N') = 1"),
})
public class Exemplar implements Serializable
{
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @SequenceGenerator (allocationSize = 1, name = "seg", sequenceName = "SEQ_EXEMPLAR")
    @GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "seg")
    @Id
    @Basic (optional = false)
    @NotNull
    @Column (name = "ID")
    private BigDecimal id;
    @Basic (optional = false)
    @NotNull
    @Size (min = 1, max = 200)
    @Column (name = "BARCODE")
    private String barcode;
    @OneToMany (mappedBy = "exemplar")
    private Collection<Lease> leases;
    @JoinColumn (name = "BOOK_ID", referencedColumnName = "ID")
    @ManyToOne (optional = false)
    private Book book;

    public Exemplar()
    {
    }

    public Exemplar(BigDecimal id)
    {
        this.id = id;
    }

    public Exemplar(BigDecimal id, String barcode)
    {
        this.id = id;
        this.barcode = barcode;
    }

    public BigDecimal getId()
    {
        return id;
    }

    public void setId(BigDecimal id)
    {
        this.id = id;
    }

    public String getBarcode()
    {
        return barcode;
    }

    public void setBarcode(String barcode)
    {
        this.barcode = barcode;
    }

    @XmlTransient
    public Collection<Lease> getLeases()
    {
        return leases;
    }

    public void setLeases(Collection<Lease> leaseCollection)
    {
        this.leases = leaseCollection;
    }

    public Book getBook()
    {
        return book;
    }

    public void setBook(Book book)
    {
        this.book = book;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Exemplar))
        {
            return false;
        }
        Exemplar other = (Exemplar) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return String.format("Id %s, barcode %s", id, barcode);
    }
}
